﻿package batchload;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test800 {
    //전역변수
    Connection          conn            = null;
    Statement           statement       = null;    
    int                 TEST_VALUE      = 0;
    String              tableName       = null;
    String              inputData       = null;
    int                 columCount      = 0;
    int                 loopCount       = 0;
    
    
    //기본생성자
    public Test800(){
        TEST_VALUE      = 10000;
        columCount      = 100;
        tableName       = "TEST_TABLE_100_1";
        inputData       = "ABCDEFGHIJ";
    }

    //DB접속
    public void DBConn(){
        try {
            //Sybase ASE
            Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
            conn = DriverManager.getConnection( "jdbc:sybase:Tds:58.76.19.47:5000/DMASIASE", "aisase","aisase");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block            
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //sql 작성
    public String makeSql(String tableName, int index, String value, int columCount){
        StringBuilder SQL = new StringBuilder();
        
        SQL.append("INSERT INTO ");
        SQL.append(tableName);
        SQL.append(" VALUES (");
        
        for(int i = 0 ; i < columCount ; i++){
            if(i == 0 ) {
                SQL.append(" " + index +" ");
            }else{
                SQL.append(" , '" + value + "' ");
            }
        }
        SQL.append(" ) ");

        return SQL.toString();
    }//sql 작성 End
    
    //table 초기화
    public void clearTable(String tableName){
        String clear_sql = null;
        
        try {
            statement = conn.createStatement() ;
            clear_sql ="TRUNCATE TABLE " + tableName;
            statement.execute(clear_sql) ;
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("SQL error");
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("error");
        }
    }//table 초기화 End
    
    //TABLE ROW COUNT CHECK
    public void selectCount(String tableName){
        String select_sql = null;
        ResultSet rs = null;
        int     rowCount = 0;
        
        try {
            statement = conn.createStatement() ;
            select_sql ="SELECT COUNT(1) FROM " + tableName;
            
            rs = statement.executeQuery(select_sql) ;
            
            while(rs.next()){
                rowCount = rs.getInt(1);
            }
            System.out.println("rowCount : "+ rowCount);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("SQL error");
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("error");
        }
    }//TABLE ROW COUNT CHECK END
    
    //건by건
    public void excuteQuery(){
        String  SQL = null;
        
        try {
            statement = conn.createStatement();
            long stime = System.currentTimeMillis(); //시작시간
            for(int i = 0 ; i < TEST_VALUE ; i++){
                SQL = makeSql(tableName, i, inputData, columCount);
                statement.executeUpdate(SQL);
            }
            long etime  = System.currentTimeMillis(); //종료시간
            System.out.println("Total ruunig time : " + (etime - stime) + "ms");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("SQL error");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("error");
        }
    }
    
    
    //MAIN
    public static void main(String[] args){     
        System.out.println("============================");
        Test800 ob1 = new Test800();
        ob1.DBConn();
        
        ob1.excuteQuery();
        
        ob1.selectCount(ob1.tableName);        
        //ob1.clearTable(ob1.tableName);
    }
}
